//
//  ViewController.m
//  UI24_Work
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "NSString+YHMD5.h"
#import "AFNetworking.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *jiaMiTextField;
@property (strong, nonatomic) IBOutlet UITextField *jieMiTextField;
@property (strong, nonatomic) IBOutlet UIButton *jiaMiButton;

@property (strong, nonatomic) IBOutlet UIButton *jieMiButton;

@property (strong, nonatomic) IBOutlet UIButton *changeButton;

@end

@implementation ViewController

- (IBAction)jiaMiAction:(id)sender {
//    NSString *str = self.jiaMiTextField.text;
//    NSString *str1 = [str stringWith16BitMD5];
//    self.jiaMiTextField.text = str1;
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    NSString *postStr = @"http://tool.zzblo.com/api/md5/encrypt";
    NSString *parmeters = [NSString stringWithFormat:@"text=%@", self.jiaMiTextField.text];
    [sessionManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    [sessionManager POST:postStr parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        self.jieMiTextField.text = responseObject[@"secret16"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
    self.jiaMiTextField.text = @"";

}
- (IBAction)jieMiAction:(id)sender {
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    NSString *postStr = @"http://tool.zzblo.com/api/md5/decrypt";
    NSString *parmeters = [NSString stringWithFormat:@"secret=%@", self.jieMiTextField.text];
    [sessionManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    [sessionManager POST:postStr parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"ssssss%@", responseObject);
        if (![responseObject[@"mesg"] isEqualToString:@"密文不符合规范"]) {
            self.jiaMiTextField.text = responseObject[@"text"];
            self.jieMiTextField.text = @"";
        } else {
        self.jiaMiTextField.text = @"";
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
- (IBAction)changeButton:(id)sender {
    NSString *str = self.jieMiTextField.text;
    NSString *str1 = [str stringWith16BitMD5];
    self.jieMiTextField.text = str1;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
